package com.wdg.dao.impl;

import com.wdg.pojo.Student_info;
import com.wdg.pojo.UserVO;
import com.wdg.dao.StudentInfoDao;
import com.wdg.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentInfoImpl implements StudentInfoDao {
    @Override
    public List<Student_info> select_UserInfo(UserVO uv) {
        Connection conn = null;
        PreparedStatement psta = null;
        ResultSet rs = null;
        ArrayList<Student_info> list = new ArrayList<Student_info>();
        DBUtil util = new DBUtil();
        // String sql="select * from student_reg where ? like ?";
        String sql = "select id, username, sex, address, score from student_info where username like ? and " +
                "address like ? and " +
                "score " + uv.getScore_cmp() + " ? and " +
                "sex = ?";
        try {
            conn = util.getCn();
            psta = conn.prepareStatement(sql);
            psta.setString(1, "%" + uv.getUsername() + "%");
            psta.setString(2, "%" + uv.getAddress() + "%");
            psta.setString(3, uv.getScore_cmp());
            psta.setString(4, uv.getSex());
            rs = psta.executeQuery();
            while (rs.next()) {
                Student_info u = new Student_info();
                u.setId(rs.getInt(1));
                u.setUsername(rs.getString(2));
                u.setSex(rs.getString(3));
                u.setAddress(rs.getString(4));
                u.setScore(rs.getInt(5));
                list.add(u);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            util.closeConn(conn);
        }
        return list;
    }

}
